using System;
using System.Collections.Generic;
using System.Linq;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using System.ComponentModel.DataAnnotations;
using HengTong.Model.Db;
using HengTong.Model.Db.Hr;


namespace HengTong.ViewModel.Person.SysPersonProfessionalTitleVms
{
    public partial class
        SysPersonProfessionalTitleListVm : BasePagedListVM<SysPersonProfessionalTitleView,
        SysPersonProfessionalTitleSearcher>
    {
        protected override IEnumerable<IGridColumn<SysPersonProfessionalTitleView>> InitGridHeader()
        {
            return new List<GridColumn<SysPersonProfessionalTitleView>>
            {
                this.MakeGridHeader(x => x.PersonName),
                this.MakeGridHeader(x => x.ProfessionalTitleTypeName),
                this.MakeGridHeader(x => x.QualificationName),
                this.MakeGridHeader(x => x.QualificationLevel),
                this.MakeGridHeader(x => x.IssuingAuthority),
                this.MakeGridHeader(x => x.CertificateCode),
                this.MakeGridHeader(x => x.ObtainedDate),
                this.MakeGridHeader(x => x.ExpiryDate),
                this.MakeGridHeader(x => x.SalaryReward),
                this.MakeGridHeader(x => x.CertificateDocumentId).SetFormat(CertificateDocumentIdFormat),
                this.MakeGridHeader(x => x.Remark),
                this.MakeGridHeaderAction(width: 200)
            };
        }

        private List<ColumnFormatInfo> CertificateDocumentIdFormat(SysPersonProfessionalTitleView entity, object val)
        {
            return new List<ColumnFormatInfo>
            {
                ColumnFormatInfo.MakeDownloadButton(ButtonTypesEnum.Button, entity.CertificateDocumentId),
                ColumnFormatInfo.MakeViewButton(ButtonTypesEnum.Button, entity.CertificateDocumentId, 640, 480),
            };
        }


        public override IOrderedQueryable<SysPersonProfessionalTitleView> GetSearchQuery()
        {
            var query = DC.Set<SysPersonProfessionalTitle>()
                .CheckEqual(Searcher.PersonId, x => x.PersonId)
                .CheckEqual(Searcher.ProfessionalTitleTypeId, x => x.ProfessionalTitleTypeId)
                .CheckContain(Searcher.QualificationName, x => x.QualificationName)
                .CheckEqual(Searcher.QualificationLevel, x => x.QualificationLevel)
                .CheckContain(Searcher.IssuingAuthority, x => x.IssuingAuthority)
                .CheckContain(Searcher.CertificateCode, x => x.CertificateCode)
                .CheckBetween(Searcher.ObtainedDate?.GetStartTime(), Searcher.ObtainedDate?.GetEndTime(),
                    x => x.ObtainedDate, includeMax: false)
                .CheckBetween(Searcher.ExpiryDate?.GetStartTime(), Searcher.ExpiryDate?.GetEndTime(), x => x.ExpiryDate,
                    includeMax: false)
                .CheckEqual(Searcher.SalaryReward, x => x.SalaryReward)
                .CheckContain(Searcher.Remark, x => x.Remark)
                .Select(x => new SysPersonProfessionalTitleView
                {
                    ID = x.ID,
                    PersonName = x.Person.Name,
                    ProfessionalTitleTypeName = x.ProfessionalTitleType.CategoryName,
                    QualificationName = x.QualificationName,
                    QualificationLevel = x.QualificationLevel,
                    IssuingAuthority = x.IssuingAuthority,
                    CertificateCode = x.CertificateCode,
                    ObtainedDate = x.ObtainedDate,
                    ExpiryDate = x.ExpiryDate,
                    SalaryReward = x.SalaryReward,
                    CertificateDocumentId = x.CertificateDocumentId,
                    Remark = x.Remark,
                })
                .OrderBy(x => x.ObtainedDate ?? DateTime.MaxValue);
            return query;
        }
    }

    public class SysPersonProfessionalTitleView : SysPersonProfessionalTitle
    {
        [Display(Name = "姓名")] public String PersonName { get; set; }
        [Display(Name = "类别名称")] public String ProfessionalTitleTypeName { get; set; }
    }
}